package me.xpyex.software.checkclassmates;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner;

public class Main {
    private static final Scanner INPUT = new Scanner(System.in);
    private static final File LIST_FOLDER = new File("Lists");
    private static ArrayList<String> USER_LIST = new ArrayList<>();
    private static ArrayList<String> CHECK_LIST = new ArrayList<>();
    private static String fileName;

    public static void main(String[] args) {
        try {
            System.out.println("正在初始化");
            if (!LIST_FOLDER.exists()) {
                LIST_FOLDER.mkdirs();
            }

            int folderSize = LIST_FOLDER.list() != null ? LIST_FOLDER.list().length : 0;
            if (folderSize == 0) {
                System.out.println("你还没有提供名单，请提供名单到 " + LIST_FOLDER.getPath() + " 目录 [名单必须文本文件(.txt文件)才可读取]");
                System.exit(0);
                return;
            }

            File CLASSMATES_LIST_FILE;
            if (folderSize == 1) {
                CLASSMATES_LIST_FILE = LIST_FOLDER.listFiles()[0];
                System.out.println("名单目录中只有一个文件，已自动选择该文件为本次点名的名单");
            } else {
                System.out.println("名单目录中有数个文件，请您选择其中一份作为本次点名的名单");
                for (int i = 0; i < folderSize; i++) {
                    System.out.println("[" + (i + 1) + "] " + LIST_FOLDER.listFiles()[i].getName());
                }
                System.out.println("输入文件名前方的序号即可");
                System.out.println("输入 cancel 以取消任务");
                System.out.println(" ");
                while (true) {
                    String id = INPUT.nextLine();
                    if (id.trim().isEmpty()) {
                        System.out.println("不想干就说不想干了，啥都不写是什么意思啊喂!");
                        continue;
                    }
                    if (id.equalsIgnoreCase("cancel")) {
                        System.out.println("任务取消，程序结束");
                        System.exit(0);
                        return;
                    }
                    try {
                        CLASSMATES_LIST_FILE = LIST_FOLDER.listFiles()[Integer.parseInt(id) - 1];
                        break;
                    } catch (NumberFormatException ignored) {
                        System.out.println("你输入的序号并不是整数，请重新输入");
                    } catch (IndexOutOfBoundsException ignored) {
                        System.out.println("你输入的序号并不存在，请重新输入");
                    }
                }
            }
            fileName = CLASSMATES_LIST_FILE.getName().contains(".") ? CLASSMATES_LIST_FILE.getName() : CLASSMATES_LIST_FILE.getName() + ".txt";

            USER_LIST.addAll(Files.readAllLines(CLASSMATES_LIST_FILE.toPath(), StandardCharsets.UTF_8));
            System.out.println("成功读取名单内的 " + USER_LIST.size() + " 个同学");
            System.out.println(" ");

            System.out.println("请输入需要检查的名单，自动进行比对，或输入 cancel 以取消检查");
            System.out.println(" ");
            inputCheck();
            System.out.println(" ");

            System.out.println("已读取需要检查的名单，共 " + CHECK_LIST.size() + " 人");
            System.out.println(" ");

            while (!check()) {  //当有未点到名的同学出现
                System.out.println("如需继续点名，请输入新的名字，或输入 cancel 以终止点名");
                System.out.println("如需保存当前剩余名单，请输入 save");
                System.out.println(" ");
                System.out.println(" ");
                System.out.println(" ");
                inputCheck();
            }

            System.out.println("已完成");

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("出现错误 " + e + " ,程序已终止");
            System.exit(0);
        }
    }

    public static void inputCheck() {
        String in = INPUT.nextLine();
        if (in.trim().isEmpty()) {
            System.out.println("你什么都没输入啊喂");
            inputCheck();
            return;
        }
        if (in.equalsIgnoreCase("save")) {
            saveCurrent();
            inputCheck();
        } else if (in.equalsIgnoreCase("cancel")) {
            System.out.println("已取消检查，程序退出");
            System.exit(0);
        } else if (in.contains("接龙")) {
            for (String s : in.split("\\.")) {
                if (s.trim().isEmpty() || s.startsWith("#接龙")) {
                    continue;
                }
                CHECK_LIST.add(s.replaceAll("[0-9]", ""));
            }
        } else {
            CHECK_LIST.addAll(Arrays.asList(in.split("\n")));
        }
    }

    public static boolean check() {
        System.out.println(" ");
        System.out.println("开始检查");
        ArrayList<String> clonedCheckList = new ArrayList<>(CHECK_LIST);
        ArrayList<String> clonedUserList = new ArrayList<>(USER_LIST);

        for (String classmate : USER_LIST) {
            for (String forCheck : CHECK_LIST) {
                if (classmate.equals(forCheck) || forCheck.contains(classmate)) {
                    clonedUserList.remove(classmate);
                    clonedCheckList.remove(forCheck);
                }
            }
        }
        System.out.println("检查完毕");
        System.out.println(" ");
        CHECK_LIST.clear();
        USER_LIST = clonedUserList;

        if (!clonedCheckList.isEmpty()) {
            System.out.println("待检查名单中存在意外的成员: ");
            for (String s : clonedCheckList) {
                System.out.println(s);
            }
            System.out.println(" ");
        }

        if (!clonedUserList.isEmpty()) {
            System.out.println("剩余 " + clonedUserList.size() + " 位同学未被点到名: ");
            for (String s : clonedUserList) {
                System.out.println(s);
            }
            System.out.println(" ");
            return false;
        } else {
            System.out.println("所有同学都已到位，程序退出");
            System.exit(0);
            return true;
        }
    }

    public static void saveCurrent() {
        File outFolder = new File("Checked");
        if (!outFolder.exists()) {
            outFolder.mkdirs();
        }
        //分割fileName里面的文件名和文件类型
        String name = fileName.substring(0, fileName.lastIndexOf("."));
        String type = fileName.substring(fileName.lastIndexOf("."));

        //新的文件
        File outFile = new File(outFolder, name + "_" + new SimpleDateFormat("yyyy-MM-dd_HH.mm.ss").format(new Date()) + type);
        try {
            outFile.createNewFile();
            Files.write(outFile.toPath(), USER_LIST, StandardCharsets.UTF_8);
            System.out.println("已保存当前剩余名单至: " + outFile.getPath());
        } catch (IOException e) {
            System.out.println("保存失败，出现错误: " + e);
            e.printStackTrace();
        }
    }
}